package com.project.x;

import java.io.File;
import java.util.ArrayList;

import org.askerov.dynamicgrid.DynamicGridView;

import com.project.x.R;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private final int OFF = 0;
	private final int ON = 1;
	
	private ArrayList<Toggle> toggles;
	private DynamicGridView gridView;
	private DynamicGridAdapter gridViewAdapter;

	private MessageHandler messageHandler = new MessageHandler();
    private FileHandler fileHandler;
    private ArrayList<MessageThread> messageThreadArrayList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		
		initializeToggles();
		
		gridView = (DynamicGridView) findViewById(R.id.dynamic_grid);
		gridViewAdapter = new DynamicGridAdapter(this, toggles, 3);
        gridView.setAdapter(gridViewAdapter);
        
        gridView.setOnDropListener(new DynamicGridView.OnDropListener()
        {
            @Override
            public void onActionDrop()
            {
                gridView.stopEditMode();
            }
        });
        
        gridView.setOnDragListener(new DynamicGridView.OnDragListener() {
            @Override
            public void onDragStarted(int position) {
                Log.d("TAG", "drag started at position " + position);
            }

            @Override
            public void onDragPositionsChanged(int oldPosition, int newPosition) {
                Log.d("TAG", String.format("drag item position changed from %d to %d", oldPosition, newPosition));
                Toggle toggle = toggles.get(oldPosition);
                toggles.remove(oldPosition);
                toggles.add(newPosition, toggle);
            }
        });
        
        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                gridView.startEditMode(position);
                return true;
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	Toggle toggle = toggles.get(position);
            	if (toggle.getState() == OFF) {
            		toggle.setState(ON);
            		Toast.makeText(MainActivity.this, toggles.get(position).getName()+" is now ON", Toast.LENGTH_SHORT).show();
            	}
            	else if (toggle.getState() == ON) {
            		toggle.setState(OFF);
            		Toast.makeText(MainActivity.this, toggles.get(position).getName()+" is now OFF", Toast.LENGTH_SHORT).show();
            	}
            	toggles.set(position, toggle);
            	gridViewAdapter.notifyDataSetChanged();
            }
        });

        messageThreadArrayList = messageHandler.extractAllDbMessages(getApplicationContext());

        fileHandler = new FileHandler(getApplicationContext());
        fileHandler.initializeThreadFile(messageThreadArrayList);
	}
	
	@Override
    public void onBackPressed() {
        if (gridView.isEditMode()) {
            gridView.stopEditMode();
        } else {
            super.onBackPressed();
        }
    }
	
	private void initializeToggles() {
		toggles = new ArrayList<Toggle>();
		
		for (int i=1; i<=12; i++) {
			toggles.add(new Toggle(OFF, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), "Toggle "+i));
		}
	}
	
	public void phone(View sender) {
		whatItDo();
	}
	
	public void sync(View sender) {
		whatItDo();
	}
	
	public void router(View sender) {
		whatItDo();
	}
	
	public void whatItDo() {
//		if (whatItDo.getVisibility() == View.GONE)
//			whatItDo.setVisibility(View.VISIBLE);
//		else {
//			Random r = new Random();
//			
//			whatItDo.measure(0, 0);
//			
//			int x = r.nextInt(space.getWidth() - whatItDo.getMeasuredWidth());
//			int y = r.nextInt(space.getHeight() - whatItDo.getMeasuredHeight());
//			
//			whatItDo.setX(x);
//			whatItDo.setY(y);
//		}
	}
}
